﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tile_Engine;

namespace AGrandVenture.Dungen
{
    public static class DungeonSkinner
    {
        //Determines which texture to use and where to draw

        private static int[,] mapToSkin;

        private static int emptyTile = 0;
        private static int floorTile = 1;
        private static int wall_AllSides = 2;
        private static int wall_Bottom_UpperLeftCap_UpperRightCap = 3;
        private static int wall_Bottom_UpperLeftCap = 4;
        private static int wall_UpperRightCap_LowerRightCap = 5;
        private static int wall_Passage_Vertical = 6;
        private static int earthTile = 7;
        private static int wall_4Caps = 8;
        private static int wall_Bottom = 9;
        private static int wall_Bottom_UpperRightCap = 10;
        private static int wall_DeadEndE = 11;
        private static int wall_Passage_Horizontal = 12;
        private static int wall_Right_UpperLeftCap_LowerLeftCap = 13;
        private static int wall_DeadEndN = 14;
        private static int wall_DeadEndS = 15;
        private static int wall_Left_LowerRightCap = 16;
        private static int wall_LowerLeftCap = 17;
        private static int wall_LowerRightCorner_UpperLeftCap = 18;
        private static int wall_Right_LowerLeftCap = 19;
        private static int wall_Right_UpperLeftCap = 20;
        private static int wall_DeadEndW = 21;
        private static int wall_Left_UpperRightCap_LowerRightCap = 22;
        private static int wall_LowerLeftCorner_UpperRightCap = 23;
        private static int wall_LowerLeftCorner = 24;
        private static int wall_LowerRightCorner = 25;
        private static int wall_Right = 26;
        private static int wall_Top_LowerLeftCap_LowerRightCap = 27;
        private static int wall_Left_UpperRightCap = 28;
        private static int wall_Left = 29;
        private static int wall_LowerRightCap = 30;
        private static int wall_LowerLeftCap_LowerRightCap = 31;
        private static int wall_UpperLeftCap_LowerRightCap = 32;
        private static int wall_Top = 33;
        private static int wall_Top_LowerLeftCap = 34;
        private static int wall_Top_LowerRightCap = 35;
        private static int wall_UpperLeftCap = 36;
        private static int wall_UpperLeftCorner = 37;
        private static int wall_LowerLeftCap_UpperLeftCap_UpperRightCap = 38;
        private static int wall_UpperRightCap = 39;
        private static int wall_UpperRightCorner_LowerLeftCap = 40;
        private static int wall_UpperRightCap_LowerLeftCap = 41;
        private static int wall_UpperLeftCorner_LowerRightCap = 42;
        private static int wall_UpperLeftCap_LowerLeftCap_LowerRightCap = 43;
        private static int wall_UpperLeftCap_LowerLeftCap = 44;
        private static int wall_UpperLeftCap_UpperRightCap = 45;
        private static int wall_UpperRightCorner = 46;
        private static int wall_LowerLeftCap_UpperRightCap_LowerRightCap = 47;
        private static int wall_UpperLeftCap_UpperRightCap_LowerRightCap = 48; 
 

        public static void SkinDungeon(int[,] dungeonToSkin)
        {
            mapToSkin = dungeonToSkin;

            placeWallTiles();
        }

        private static void placeWallTiles()
        {

            for (int x = 0; x < TileMap.MapWidth; x++)
            {
                for (int y = 0; y < TileMap.MapHeight; y++)
                {
                    //Create impassible earth border around map.
                    if (x == 0)
                        TileMap.SetMapSquareAtCell(x, y, new MapSquare(earthTile, emptyTile, emptyTile, "", false));
                    else
                    if (y == 0)
                        TileMap.SetMapSquareAtCell(x, y, new MapSquare(earthTile, emptyTile, emptyTile, "", false));
                    else
                    if (x == mapToSkin.GetLength(0))
                        TileMap.SetMapSquareAtCell(x, y, new MapSquare(earthTile, emptyTile, emptyTile, "", false));
                    else
                    if (y == mapToSkin.GetLength(1))
                        TileMap.SetMapSquareAtCell(x, y, new MapSquare(earthTile, emptyTile, emptyTile, "", false));

                    if (mapToSkin[x, y] == 0)
                    {
                        TileMap.SetMapSquareAtCell(x, y, new MapSquare(floorTile, emptyTile, emptyTile, "", true));
                    }
                    
                    if (mapToSkin[x,y] == 1)
                    {
                        //status of adjacent cell.  True = open
                        bool north = false;
                        bool south = false;
                        bool east = false;
                        bool west = false;

                        bool diagUpperLeft = false;
                        bool diagUpperRight = false;
                        bool diagLowerLeft = false;
                        bool diagLowerRight = false;

                        //Determine which tile to use.
                        if (y > 0 && y < (mapToSkin.GetLength(1) - 1) && x > 0 && x < (mapToSkin.GetLength(0) - 1)) //ensures check is not out of bounds
                        {
                            //check North Tile
                            if (mapToSkin[x,y - 1] == 0)
                                north = true;
                            //check South Tile
                            if (mapToSkin[x,y + 1] == 0)
                                south = true;
                            //check East Tile
                            if (mapToSkin[x + 1, y] == 0)
                                east = true;
                            //check West Tile
                            if (mapToSkin[x - 1, y] == 0)
                                west = true;

                            //check diagonals
                            //Upper Left
                            if (mapToSkin[x - 1, y - 1] == 0)
                                diagUpperLeft = true;
                            //Upper Right
                            if (mapToSkin[x + 1, y - 1] == 0)
                                diagUpperRight = true;
                            //Lower Left
                            if (mapToSkin[x - 1, y + 1] == 0)
                                diagLowerLeft = true;
                            //Lower Right
                            if (mapToSkin[x + 1, y + 1] == 0)
                                diagLowerRight = true;


                            if (!north && !east && !west && !south)
                            {
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(earthTile, emptyTile, emptyTile, "", false));
                                if (diagUpperLeft && !diagUpperRight && !diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperLeftCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && diagUpperRight && !diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperRightCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && !diagUpperRight && diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerLeftCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && !diagUpperRight && !diagLowerLeft && diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerRightCap, emptyTile, emptyTile, "", false));

                                if (diagUpperLeft && diagUpperRight && !diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperLeftCap_UpperRightCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && diagUpperRight && diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperRightCap_LowerLeftCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && !diagUpperRight && diagLowerLeft && diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerLeftCap_LowerRightCap, emptyTile, emptyTile, "", false));
                                if (diagUpperLeft && !diagUpperRight && !diagLowerLeft && diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperLeftCap_LowerRightCap, emptyTile, emptyTile, "", false));
                                if (diagUpperLeft && !diagUpperRight && diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperLeftCap_LowerLeftCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && diagUpperRight && !diagLowerLeft && diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperRightCap_LowerRightCap, emptyTile, emptyTile, "", false));
                            }

                            if(north && !east && !west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Top, emptyTile, emptyTile, "", false));
                            if (!north && east && !west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Right, emptyTile, emptyTile, "", false));
                            if(!north && !east && west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Left, emptyTile, emptyTile, "", false));
                            if (!north && !east && !west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Bottom, emptyTile, emptyTile, "", false));
                            if (north && east && !west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperRightCorner, emptyTile, emptyTile, "", false));
                            if (!north && east && west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Passage_Vertical, emptyTile, emptyTile, "", false));
                            if (!north && !east && west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerLeftCorner, emptyTile, emptyTile, "", false));
                            if (north && !east && !west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Passage_Horizontal, emptyTile, emptyTile, "", false));
                            if (north && !east && west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperLeftCorner, emptyTile, emptyTile, "", false));
                            if (!north && east && !west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerRightCorner, emptyTile, emptyTile, "", false));

                            if (north && east && west && !south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_DeadEndN, emptyTile, emptyTile, "", false));
                            if (!north && east && west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_DeadEndS, emptyTile, emptyTile, "", false));
                            if (north && !east && west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_DeadEndW, emptyTile, emptyTile, "", false));
                            if (north && east && !west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_DeadEndE, emptyTile, emptyTile, "", false));

                            if (north && east && west && south)
                                TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_AllSides, emptyTile, emptyTile, "", false));


                            if (north && !east && !west && !south)
                            {
                                if (diagLowerLeft && diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Top_LowerLeftCap_LowerRightCap, emptyTile, emptyTile, "", false));
                                if (diagLowerLeft && !diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Top_LowerLeftCap, emptyTile, emptyTile, "", false));
                                if (!diagLowerLeft && diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Top_LowerRightCap, emptyTile, emptyTile, "", false));
                            }

                            if (!north && east && !west && !south)
                            {
                                if (diagLowerLeft && diagUpperLeft)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Right_UpperLeftCap_LowerLeftCap, emptyTile, emptyTile, "", false));
                                if (!diagLowerLeft && diagUpperLeft)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Right_UpperLeftCap, emptyTile, emptyTile, "", false));
                                if (diagLowerLeft && !diagUpperLeft)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Right_LowerLeftCap, emptyTile, emptyTile, "", false));
                            }

                            if (!north && !east && west && !south)
                            {
                                if (diagLowerRight && diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Left_UpperRightCap_LowerRightCap, emptyTile, emptyTile, "", false));
                                if (diagLowerRight && !diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Left_LowerRightCap, emptyTile, emptyTile, "", false));
                                if (!diagLowerRight && diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Left_UpperRightCap, emptyTile, emptyTile, "", false));
                            }

                            if (!north && !east && !west && south)
                            {
                                if (diagUpperLeft && diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Bottom_UpperLeftCap_UpperRightCap, emptyTile, emptyTile, "", false));
                                if (!diagUpperLeft && diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Bottom_UpperRightCap, emptyTile, emptyTile, "", false));
                                if (diagUpperLeft && !diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_Bottom_UpperLeftCap, emptyTile, emptyTile, "", false));
                            }

                            if (north && east && !west && !south)
                                if(diagLowerLeft)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperRightCorner_LowerLeftCap, emptyTile, emptyTile, "", false));


                            if (!north && !east && west && south)
                                if(diagUpperRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerLeftCorner_UpperRightCap, emptyTile, emptyTile, "", false));

                            if (north && !east && west && !south)
                                if(diagLowerRight)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_UpperLeftCorner_LowerRightCap, emptyTile, emptyTile, "", false));

                            if (!north && east && !west && south)
                                if(diagUpperLeft)
                                    TileMap.SetMapSquareAtCell(x, y, new MapSquare(wall_LowerRightCorner_UpperLeftCap, emptyTile, emptyTile, "", false));
                            
                        }
                    }

                        //    if (mapToSkin[x, y - 1] != 0)
                        //        north = true;
                        ////Check South
                        //if (y < mapToSkin.GetLength(1))
                        //    if (mapToSkin[x, y] !=0)
                        //        south = true;
                        ////check East
                        //if (x < mapToSkin.GetLength(0))
                        //    if (mapToSkin[x, y] !=0)
                        //        east = true;
                        ////check West
                        //if (x > 0)
                        //    if (mapToSkin[x - 1, y] !=0)
                        //        west = true;

                }
            }
        }
    }
}
